#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>

#include "err_handle.h"
#include "gen_time.h"
#include "lunar.h"

#define BUF_SIZE 1024
int main(int argc, char *argv[])
{
    int sock_srv = 0;
    int sock_cln = 0;
    ssize_t write_len = 0;
    ssize_t read_len = 0;
    struct sockaddr_in serv_addr = {0};
    int rv = 0;
    int addr_opt = 1;
    char ptime[256];
    char buf[256] = "";
    char buf2[256] = "";
    char name[] = "20193417段恺宇";

    if (argc != 2)
    {
        printf("usage: %s <port> . \n", argv[0]);
        return -1;
    }
    sock_srv = socket(PF_INET, SOCK_STREAM, 0);
    if (sock_srv == -1)
    {
        error_handling("socket() error .\n");
    }

    setsockopt(sock_srv, SOL_SOCKET, SO_REUSEADDR, (void *)&addr_opt, sizeof(addr_opt));
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(atoi(argv[1]));
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //inet_addr("127.0.0.1")
    rv = bind(sock_srv, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
    if (rv != 0)
    {
        error_handling("bind() error .\n");
    }

    rv = listen(sock_srv, 5);
    if (rv != 0)
    {
        error_handling("listen() error .\n");
    }

    while (1)
    {
        sock_cln = accept(sock_srv, NULL, NULL);
        if (sock_cln == -1)
        {
            error_handling("accept() error. \n");
        }

        while (1)
        {
            memset(buf, '\0', sizeof(buf));   //清空buf缓存内容
            memset(buf2, '\0', sizeof(buf2)); //清空buf缓存内容
            read_len = read(sock_cln, buf, sizeof(buf));
            if (read_len == -1)
            {
                error_handling("read() error.");
            }
            else if (read_len > 0) //主要功能区
            {
                if (!strcmp(buf, "1\n"))
                {
                    write(sock_cln, name, sizeof(name));
                }
                else if (!strcmp(buf, "2\n"))
                {
                    gen_time(ptime);
                    write(sock_cln, ptime, sizeof(ptime));
                }
                else if (!strcmp(buf, "3\n"))
                {

                    gen_time(ptime);
                    sprintf(buf, "姓名:%s  时间:%s", name, ptime);
                    write(sock_cln, buf, sizeof(buf));
                }
                else //if(!strcmp(buf,"4\n"))
                {
                    //read(sock_cln, buf,sizeof(buf));      //删除这两行就能实现
                    lunar(buf, buf2);
                    write(sock_cln, buf2, sizeof(buf2));
                    continue;
                }
            }
            else if (read_len == 0)
            {
                break;
            }
        }
        write_len = write(sock_cln, buf, sizeof(buf));
        if (write_len == -1)
        {
            error_handling("write() error.");
        }
        close(sock_cln);
    }

    close(sock_srv);
    return 0;
}